/*********************************************************************************************************
**
**                                         SimpleBoot
**
**                                 
**
**                                  Linux 2.6(ARM)  TQ2416
**
**                               Copyright All Rights Reserved
**
**--------------文件信息------------------------------------------------------------------------------------
**
** 文   件   名: init.c
**
** 创   建   人: Yang.Jin.Long (杨金龙)
**
** 文件创建日期: 2014 年 09 月 24 日
**
** 描        述:    初始化.
                 
                 
** BUG:
2014.09.24  修正 XXXXX
*********************************************************************************************************/ 

#include "kern/types.h"
#include "kern/kern.h"
#include "io.h"

#include "s3c2416_config.h"
#include "stdio.h"

static int abortboot(int bootdelay)
{
    int abort = 0;

    printf("Hit any key to stop autoboot: %2d ", bootdelay);
	/*
	 * Check if key already pressed
	 * Don't check if bootdelay < 0
	 */
	if (bootdelay >= 0) {
	    /* we got a key press	*/
		if (tstc()) {
		    /* consume input	*/
			(void) getc();  
			printf ("\b\b\b 0");
			/* don't auto boot	*/
			abort = 1; 	
		}
	}

	while ((bootdelay > 0) && (!abort)) {
		int i;

		--bootdelay;
		/* delay 100 * 10ms */
		for (i=0; !abort && i<100; ++i) {
		    /* we got a key press	*/
			if (tstc()) {
			    /* don't auto boot	*/
				abort  = 1;
				/* no more delay	*/
				bootdelay = 0;	
                /* consume input	*/
				(void) getc();  
				break;
			}
			udelay (10000);
		}

		printf ("\b\b\b%2d ", bootdelay);
	}

	putc ('\n');
    return abort;
}

#define NAND_DMP_PAGE(func,packet,length)  \
	do { \
		int i; 							\
		printf(func ": length: %d\r\n", length);			\
		for (i = 0; i < length; i++) {				\
			if (i % 16 == 0)					\
				printf("\r\n%s: %02x: ", func, i);	\
			printf("%02x ", ((unsigned char *) packet)[i]);	\
		} printf("\r\n");						\
	} while(0)


/*********************************************************************************************************
** Function name:           main
** Descriptions:            主函数
** input parameters:        NONE
** output parameters:       NONE
** Returned value:          0 OR -1
*********************************************************************************************************/
int main(void)
{
	uint32_t *addr = (uint32_t*)ZIMAGE_BASE;
	int length = 0x400000;

    /*
     * 进入到这里 CPU 应该是关中断, 进入系统模式, MMU 被关闭,
     *
     * D-Cache 被关闭, I-Cache 可以打开
     *
     * 时钟可能未初始化好
     */
    cpuInit();

    cpuDriversInstall();
    boardDriversInstall();

    cmdInit();
    if(abortboot(1))
    {
        arch_mmu_init();        
        usbDevInit();        
        dm9000_Init();
        doShell();
    }

    nand_read_skip_bad(0x300000, &length, addr);

    //NAND_DMP_PAGE("init", addr, 0x800);
    bootLinux();
    
    for(;;);
}
/*********************************************************************************************************
  END FILE
*********************************************************************************************************/